﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web;
using System.Timers;

using GG.Data;
using GG.Mail;

namespace GG.ApplicationStatusSending
//namespace GGApplicationStatusSendingService
{
    partial class ApplicationStatusSending : ServiceBase
    {
        EventLog _eventLog = new EventLog();

        public ApplicationStatusSending()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("GGApplicationStatusSendingService"))
                System.Diagnostics.EventLog.CreateEventSource("GGApplicationStatusSendingService",
                                                                      "GG Application Status Sending Service");
            _eventLog.Source = "GGApplicationStatusSendingService";
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            timer1.Interval = 5000;
            timer1.Enabled = true;
        }

        protected override void OnStop()
        {
            timer1.Stop();
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }

        private void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer1.Enabled = false;
            string _emailTitle = "";
            string _emailBody = "";
            string _toemail = "";
            string _ccemail = "";
            string _bccemail = "";
            string _contactusemail = "";

            #region Oldvariables
            /*
            string _strOnlineFreshTotal = "0";
            string _strOnlineOldTotal = "0";
            string _strRenewalTotal = "0";
            string _strOfflineTotal = "0";
            string _strDailyTotal = "0";
            string _strMonthlyTotal = "0";

            decimal _dOnlineFreshTotal = 0;
            decimal _dOnlineOldTotal = 0;
            decimal _dRenewalTotal = 0;
            decimal _dOfflineTotal = 0;
            decimal _dDailyTotal = 0;
            decimal _dMonthlyTotal = 0;
            decimal _dDailyTotalWithCMI = 0;
            decimal _dMonthlyTotalWithCMI = 0;
             */
            #endregion

            try
            {

                DateTime _dtmApplicationDate = GG.Global.General.GetUTCDateTime();
                //_dtmApplicationDate = _dtmApplicationDate.AddDays(-1);
                string IsDate = ConfigurationManager.AppSettings["IsDate"].ToString();
                if (IsDate.ToString() != string.Empty && IsDate == "1")
                {
                    string _strApplicationDate = ConfigurationManager.AppSettings["AppDate"].ToString();
                    int _iDay = Convert.ToInt32(_strApplicationDate.Substring(0, 2));
                    int _iMonth = Convert.ToInt32(_strApplicationDate.Substring(2, 2));
                    int _iYear = Convert.ToInt32(_strApplicationDate.Substring(4, 4));
                    _dtmApplicationDate = new DateTime(_iYear, _iMonth, _iDay);
                }
                _emailTitle = "GG Sales @ " + _dtmApplicationDate.ToString("dd/MM/yyyy");

                _toemail = System.Configuration.ConfigurationSettings.AppSettings["toemail"].ToString();
                _ccemail = System.Configuration.ConfigurationSettings.AppSettings["ccemail"].ToString();
                _bccemail = System.Configuration.ConfigurationSettings.AppSettings["bccemail"].ToString();
                _contactusemail = System.Configuration.ConfigurationSettings.AppSettings["contactusemail"].ToString();

                #region New Logic

                #region Variables
                string _strNewWebTotal = "0";
                string _strNewMobileTotal = "0";
                string _strUncontactTotal = "0";
                string _strPolicyNotTotal = "0";
                string _strReAssignUNTotal = "0";
                string _strOldATotal = "0";
                string _strOldBTotal = "0";
                string _strReAssignBRleftTtoal = "0";
                string _strReAssign15DaysTtoal = "0";
                string _strCallMeTtoal = "0";
                string _strCallDirectGGTtoal = "0";
                string _strCallDirectNonGGTtoal = "0";
                string _strNewRenewalTtoal = "0";
                string _strDailyVMITotal = "0";
                string _strMonthlyVMITotal = "0";
                string _strDailywithCMITotal = "0";
                string _strMonthlywithCMITotal = "0";
                string _strPromotionCallMeTtoal = "0";
                string _strPromotionCallMeOtherTtoal = "0";
                string _strMonthlyCancelVMITotal = "0";
                string _strMonthlyCancelwithCMITotal = "0";
                string _strFacebookDailyTotal = "0";
                string _strGoogleDailyTotal = "0";
                string _strFacebookMonthlyTotal = "0";
                string _strGoogleMonthlyTotal = "0";

                decimal _dNewWebTotal = 0;
                decimal _dNewMobileTotal = 0;
                decimal _dUncontactTotal = 0;
                decimal _dPolicyNotTotal = 0;
                decimal _dReAssignUNTotal = 0;
                decimal _dOldATotal = 0;
                decimal _dOldBTotal = 0;
                decimal _dReAssignBRleftTtoal = 0;
                decimal _dReAssign15DaysTtoal = 0;
                decimal _dCallMeTtoal = 0;
                decimal _dCallDirectGGTtoal = 0;
                decimal _dCallDirectNonGGTtoal = 0;
                decimal _dNewRenewalTotal = 0;
                decimal _dDailyVMITotal = 0;
                decimal _dMonthlyVMITotal = 0;
                decimal _dDailywithCMITotal = 0;
                decimal _dMonthlywithCMITotal = 0;
                decimal _dPromotionCallMeTtoal = 0;
                decimal _dPromotionCallMeOtherTtoal = 0;
                decimal _dMonthlyCancelVMITotal = 0;
                decimal _dMonthlyCancelwithCMITotal = 0;
                decimal _dFacebookDailyTotal = 0;
                decimal _dGoogleDailyTotal = 0;
                decimal _dFacebookMonthlyTotal = 0;
                decimal _dGoogleMonthlyTotal = 0;
                Int32 _NewCount = 0;
                #endregion

                MyData myNewdata = new MyData();
                DataSet _dsNewApplicationStatus = new DataSet();
                _dsNewApplicationStatus = myNewdata.GetApplicationStatusList();
                if (_dsNewApplicationStatus != null && _dsNewApplicationStatus.Tables.Count > 0)
                {
                    #region Get Count
                    // NEW (Fresh Leads-web)
                    if (_dsNewApplicationStatus.Tables.Count > 0 && _dsNewApplicationStatus.Tables[0].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[0].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 1 && _dsNewApplicationStatus.Tables[1].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[1].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 2 && _dsNewApplicationStatus.Tables[2].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[2].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 3 && _dsNewApplicationStatus.Tables[3].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[3].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 4 && _dsNewApplicationStatus.Tables[4].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[4].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 5 && _dsNewApplicationStatus.Tables[5].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[5].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 6 && _dsNewApplicationStatus.Tables[6].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[6].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 7 && _dsNewApplicationStatus.Tables[7].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[7].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 8 && _dsNewApplicationStatus.Tables[8].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[8].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 9 && _dsNewApplicationStatus.Tables[9].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[9].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 10 && _dsNewApplicationStatus.Tables[10].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[10].Rows.Count;
                    }
                    if (_dsNewApplicationStatus.Tables.Count > 11 && _dsNewApplicationStatus.Tables[11].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[11].Rows.Count;
                    }
                    // Promotion
                    if (_dsNewApplicationStatus.Tables.Count > 12 && _dsNewApplicationStatus.Tables[13].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[13].Rows.Count;
                    }
                    // NEW (Fresh Leads-mobile)
                    if (_dsNewApplicationStatus.Tables.Count > 13 && _dsNewApplicationStatus.Tables[14].Rows.Count > 0)
                    {
                        _NewCount += _dsNewApplicationStatus.Tables[14].Rows.Count;
                    }
                    #endregion

                    #region Daily and Monthly Total Collection - Values
                    if (_dsNewApplicationStatus.Tables.Count > 12 && _dsNewApplicationStatus.Tables[12].Rows.Count > 0)
                    {
                        // NEW (Fresh Leads-web)
                        if (_dsNewApplicationStatus.Tables[12].Rows[0]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[0]["Amount"].ToString() != string.Empty)
                        {
                            _strNewWebTotal = _dsNewApplicationStatus.Tables[12].Rows[0]["Amount"].ToString();
                            if (!decimal.TryParse(_strNewWebTotal, out _dNewWebTotal))
                            {
                                _dNewWebTotal = 0;
                            }
                        }
                        // NEW (Fresh Leads-mobile)
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 20 && _dsNewApplicationStatus.Tables[12].Rows[20]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[20]["Amount"].ToString() != string.Empty)
                        {
                            _strNewMobileTotal = _dsNewApplicationStatus.Tables[12].Rows[20]["Amount"].ToString();
                            if (!decimal.TryParse(_strNewMobileTotal, out _dNewMobileTotal))
                            {
                                _dNewMobileTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 1 && _dsNewApplicationStatus.Tables[12].Rows[1]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[1]["Amount"].ToString() != string.Empty)
                        {
                            _strUncontactTotal = _dsNewApplicationStatus.Tables[12].Rows[1]["Amount"].ToString();
                            if (!decimal.TryParse(_strUncontactTotal, out _dUncontactTotal))
                            {
                                _dUncontactTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 2 && _dsNewApplicationStatus.Tables[12].Rows[2]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[2]["Amount"].ToString() != string.Empty)
                        {
                            _strPolicyNotTotal = _dsNewApplicationStatus.Tables[12].Rows[2]["Amount"].ToString();
                            if (!decimal.TryParse(_strPolicyNotTotal, out _dPolicyNotTotal))
                            {
                                _dPolicyNotTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 3 && _dsNewApplicationStatus.Tables[12].Rows[3]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[3]["Amount"].ToString() != string.Empty)
                        {
                            _strReAssignUNTotal = _dsNewApplicationStatus.Tables[12].Rows[3]["Amount"].ToString();
                            if (!decimal.TryParse(_strReAssignUNTotal, out _dReAssignUNTotal))
                            {
                                _dReAssignUNTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 4 && _dsNewApplicationStatus.Tables[12].Rows[4]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[3]["Amount"].ToString() != string.Empty)
                        {
                            _strOldATotal = _dsNewApplicationStatus.Tables[12].Rows[4]["Amount"].ToString();
                            if (!decimal.TryParse(_strOldATotal, out _dOldATotal))
                            {
                                _dOldATotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 5 && _dsNewApplicationStatus.Tables[12].Rows[5]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[5]["Amount"].ToString() != string.Empty)
                        {
                            _strOldBTotal = _dsNewApplicationStatus.Tables[12].Rows[5]["Amount"].ToString();
                            if (!decimal.TryParse(_strOldBTotal, out _dOldBTotal))
                            {
                                _dOldBTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 6 && _dsNewApplicationStatus.Tables[12].Rows[6]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[6]["Amount"].ToString() != string.Empty)
                        {
                            _strReAssignBRleftTtoal = _dsNewApplicationStatus.Tables[12].Rows[6]["Amount"].ToString();
                            if (!decimal.TryParse(_strReAssignBRleftTtoal, out _dReAssignBRleftTtoal))
                            {
                                _dReAssignBRleftTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 7 && _dsNewApplicationStatus.Tables[12].Rows[7]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[7]["Amount"].ToString() != string.Empty)
                        {
                            _strReAssign15DaysTtoal = _dsNewApplicationStatus.Tables[12].Rows[7]["Amount"].ToString();
                            if (!decimal.TryParse(_strReAssign15DaysTtoal, out _dReAssign15DaysTtoal))
                            {
                                _dReAssign15DaysTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 8 && _dsNewApplicationStatus.Tables[12].Rows[8]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[8]["Amount"].ToString() != string.Empty)
                        {
                            _strCallMeTtoal = _dsNewApplicationStatus.Tables[12].Rows[8]["Amount"].ToString();
                            if (!decimal.TryParse(_strCallMeTtoal, out _dCallMeTtoal))
                            {
                                _dCallMeTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 9 && _dsNewApplicationStatus.Tables[12].Rows[9]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[9]["Amount"].ToString() != string.Empty)
                        {
                            _strCallDirectGGTtoal = _dsNewApplicationStatus.Tables[12].Rows[9]["Amount"].ToString();
                            if (!decimal.TryParse(_strCallDirectGGTtoal, out _dCallDirectGGTtoal))
                            {
                                _dCallDirectGGTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 10 && _dsNewApplicationStatus.Tables[12].Rows[10]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[10]["Amount"].ToString() != string.Empty)
                        {
                            _strCallDirectNonGGTtoal = _dsNewApplicationStatus.Tables[12].Rows[10]["Amount"].ToString();
                            if (!decimal.TryParse(_strCallDirectNonGGTtoal, out _dCallDirectNonGGTtoal))
                            {
                                _dCallDirectNonGGTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 11 && _dsNewApplicationStatus.Tables[12].Rows[11]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[11]["Amount"].ToString() != string.Empty)
                        {
                            _strNewRenewalTtoal = _dsNewApplicationStatus.Tables[12].Rows[11]["Amount"].ToString();
                            if (!decimal.TryParse(_strNewRenewalTtoal, out _dNewRenewalTotal))
                            {
                                _dNewRenewalTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 12 && _dsNewApplicationStatus.Tables[12].Rows[12]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[12]["Amount"].ToString() != string.Empty)
                        {
                            _strDailyVMITotal = _dsNewApplicationStatus.Tables[12].Rows[12]["Amount"].ToString();
                            if (!decimal.TryParse(_strDailyVMITotal, out _dDailyVMITotal))
                            {
                                _dDailyVMITotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 13 && _dsNewApplicationStatus.Tables[12].Rows[13]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[13]["Amount"].ToString() != string.Empty)
                        {
                            _strDailywithCMITotal = _dsNewApplicationStatus.Tables[12].Rows[13]["Amount"].ToString();
                            if (!decimal.TryParse(_strDailywithCMITotal, out _dDailywithCMITotal))
                            {
                                _dDailywithCMITotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 14 && _dsNewApplicationStatus.Tables[12].Rows[14]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[14]["Amount"].ToString() != string.Empty)
                        {
                            _strMonthlyVMITotal = _dsNewApplicationStatus.Tables[12].Rows[14]["Amount"].ToString();
                            if (!decimal.TryParse(_strMonthlyVMITotal, out _dMonthlyVMITotal))
                            {
                                _dMonthlyVMITotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 15 && _dsNewApplicationStatus.Tables[12].Rows[15]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[15]["Amount"].ToString() != string.Empty)
                        {
                            _strMonthlywithCMITotal = _dsNewApplicationStatus.Tables[12].Rows[15]["Amount"].ToString();
                            if (!decimal.TryParse(_strMonthlywithCMITotal, out _dMonthlywithCMITotal))
                            {
                                _dMonthlywithCMITotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 20 && _dsNewApplicationStatus.Tables[12].Rows[21]["NoOfSales"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[21]["NoOfSales"].ToString() != string.Empty)
                        {
                            _strFacebookDailyTotal = _dsNewApplicationStatus.Tables[12].Rows[21]["NoOfSales"].ToString();
                            if (!decimal.TryParse(_strFacebookDailyTotal, out _dFacebookDailyTotal))
                            {
                                _dFacebookDailyTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 21 && _dsNewApplicationStatus.Tables[12].Rows[22]["NoOfSales"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[22]["NoOfSales"].ToString() != string.Empty)
                        {
                            _strFacebookMonthlyTotal = _dsNewApplicationStatus.Tables[12].Rows[22]["NoOfSales"].ToString();
                            if (!decimal.TryParse(_strFacebookMonthlyTotal, out _dFacebookMonthlyTotal))
                            {
                                _dFacebookMonthlyTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 22 && _dsNewApplicationStatus.Tables[12].Rows[23]["NoOfSales"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[23]["NoOfSales"].ToString() != string.Empty)
                        {
                            _strGoogleDailyTotal = _dsNewApplicationStatus.Tables[12].Rows[23]["NoOfSales"].ToString();
                            if (!decimal.TryParse(_strGoogleDailyTotal, out _dGoogleDailyTotal))
                            {
                                _dGoogleDailyTotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 23 && _dsNewApplicationStatus.Tables[12].Rows[24]["NoOfSales"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[24]["NoOfSales"].ToString() != string.Empty)
                        {
                            _strGoogleMonthlyTotal = _dsNewApplicationStatus.Tables[12].Rows[24]["NoOfSales"].ToString();
                            if (!decimal.TryParse(_strGoogleMonthlyTotal, out _dGoogleMonthlyTotal))
                            {
                                _dGoogleMonthlyTotal = 0;
                            }
                        }
                        // Promotion Call Me
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 16 && _dsNewApplicationStatus.Tables[12].Rows[16]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[16]["Amount"].ToString() != string.Empty)
                        {
                            _strPromotionCallMeTtoal = _dsNewApplicationStatus.Tables[12].Rows[16]["Amount"].ToString();
                            if (!decimal.TryParse(_strPromotionCallMeTtoal, out _dPromotionCallMeTtoal))
                            {
                                _dPromotionCallMeTtoal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 17 && _dsNewApplicationStatus.Tables[12].Rows[17]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[17]["Amount"].ToString() != string.Empty)
                        {
                            _strPromotionCallMeOtherTtoal = _dsNewApplicationStatus.Tables[12].Rows[17]["Amount"].ToString();
                            if (!decimal.TryParse(_strPromotionCallMeOtherTtoal, out _dPromotionCallMeOtherTtoal))
                            {
                                _dPromotionCallMeOtherTtoal = 0;
                            }
                        }
                        // Cancel
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 18 && _dsNewApplicationStatus.Tables[12].Rows[18]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[18]["Amount"].ToString() != string.Empty)
                        {
                            _strMonthlyCancelVMITotal = _dsNewApplicationStatus.Tables[12].Rows[18]["Amount"].ToString();
                            if (!decimal.TryParse(_strMonthlyCancelVMITotal, out _dMonthlyCancelVMITotal))
                            {
                                _dMonthlyCancelVMITotal = 0;
                            }
                        }
                        if (_dsNewApplicationStatus.Tables[12].Rows.Count > 19 && _dsNewApplicationStatus.Tables[12].Rows[19]["Amount"] != DBNull.Value
                                && _dsNewApplicationStatus.Tables[12].Rows[19]["Amount"].ToString() != string.Empty)
                        {
                            _strMonthlyCancelwithCMITotal = _dsNewApplicationStatus.Tables[12].Rows[19]["Amount"].ToString();
                            if (!decimal.TryParse(_strMonthlyCancelwithCMITotal, out _dMonthlyCancelwithCMITotal))
                            {
                                _dMonthlyCancelwithCMITotal = 0;
                            }
                        }
                    }
                    #endregion

                    if (_NewCount > 0)
                    {
                        #region Email Body
                        _emailBody = "";
                        _emailBody += "<html>";
                        _emailBody += "<head>";
                        _emailBody += "<title></title>";
                        _emailBody += "</head>";
                        _emailBody += "<body class='email_body'>";

                        _emailBody += "<style type='text/css'>";
                        _emailBody += ".email_body {background: white;margin: 0;padding: 0;color: #4c4c4c;font-family: Arial, Helvetica, sans-serif;font-size: 0.85em;}";
                        _emailBody += "a {text-decoration:underline;color:#2c95cc;} ";
                        _emailBody += "#email_container {margin:0 auto;padding:20px;width:600px; height:auto;} ";
                        _emailBody += "#email_body {width:100%; text-align: left !important;} ";
                        _emailBody += ".email_pad10px {padding:10px;} ";
                        _emailBody += ".email_results td {width:33%;} ";
                        _emailBody += "</style>";

                        _emailBody += "<table width='100%' border='0' cellspacing='2' cellpadding='0'>";
                        _emailBody += "<tr><td style='color:green; font-size:large;'><b>GG Sales at " + _dtmApplicationDate.ToString("dd/MMM/yyyy dddd") + "</b></td></tr>";
                        _emailBody += "<tr><td>&nbsp;</td></tr>";

                        #region NEW (Fresh Leads-web)
                        if (_dsNewApplicationStatus.Tables[0].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>NEW (Fresh Leads-web)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[0].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[0].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[0].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[0].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[0].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[0].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dNewWebTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region NEW (Fresh Leads-mobile)
                        if (_dsNewApplicationStatus.Tables[14].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>NEW (Fresh Leads-mobile)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[14].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[14].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[14].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[14].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[14].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[14].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dNewMobileTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region UN-Contacted Leads
                        if (_dsNewApplicationStatus.Tables[1].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>UN-Contacted Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[1].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[1].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[1].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dUncontactTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Re-assign Policy Not Expire Soon
                        if (_dsNewApplicationStatus.Tables[2].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Re-assign Policy Not Expire Soon</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[2].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[2].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[2].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[2].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[2].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[2].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dPolicyNotTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Re-assign Un-Contact Leads
                        if (_dsNewApplicationStatus.Tables[3].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Re-assign Un-Contacted Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[2].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[3].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[3].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[3].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[3].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[3].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dReAssignUNTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Old Leads - A
                        if (_dsNewApplicationStatus.Tables[4].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Old Leads (A) Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[4].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[4].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[4].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dOldATotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Old Leads - B
                        if (_dsNewApplicationStatus.Tables[5].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Old Leads (B) Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[5].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[5].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[5].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[5].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[5].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[5].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dOldBTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Re-assign Call Backs (Broker - Who Left)
                        if (_dsNewApplicationStatus.Tables[6].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Re-assign Call Backs (Broker - Who Left)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[6].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[6].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[6].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[6].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[6].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[6].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dReAssignBRleftTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Re-assign Call Backs (Not Completed in 15 working days)
                        if (_dsNewApplicationStatus.Tables[7].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Re-assign Call Backs (Not Completed in 15 working days)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[7].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[7].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[7].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[7].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[7].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[7].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dReAssign15DaysTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Call Me
                        if (_dsNewApplicationStatus.Tables[8].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Call Me Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[8].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[8].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[8].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[8].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[8].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[8].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dCallMeTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Promotion Call Me
                        if (_dsNewApplicationStatus.Tables[13].Select("InsuranceType = 'Class 1' AND CompanyName = 'Syn Mun Kong'").Length > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Promotion Call Me Sales (Syn Mun Kong)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            foreach (DataRow _drP in _dsNewApplicationStatus.Tables[13].Select("InsuranceType = 'Class 1' AND CompanyName = 'Syn Mun Kong'"))
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_drP["TotalCollectedPremium"] != DBNull.Value && _drP["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_drP["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _drP["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _drP["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dPromotionCallMeTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }

                        if (_dsNewApplicationStatus.Tables[13].Select("InsuranceType <> 'Class 1' OR CompanyName <> 'Syn Mun Kong'").Length > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Promotion Call Me Sales (Other)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            foreach (DataRow _drP in _dsNewApplicationStatus.Tables[13].Select("InsuranceType <> 'Class 1' OR CompanyName <> 'Syn Mun Kong'"))
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_drP["TotalCollectedPremium"] != DBNull.Value && _drP["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_drP["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _drP["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _drP["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dPromotionCallMeOtherTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Call Direct (GG Customer)
                        if (_dsNewApplicationStatus.Tables[9].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Call Direct (GG Customer)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[9].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[9].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[9].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[9].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[9].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[9].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dCallDirectGGTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Call Direct (Not visit to GG before Call)
                        if (_dsNewApplicationStatus.Tables[10].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Call Direct (Not visit to GG before Call)</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[10].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[10].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[10].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[10].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[10].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[10].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dCallDirectNonGGTtoal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Renewal Leads
                        if (_dsNewApplicationStatus.Tables[11].Rows.Count > 0)
                        {
                            _emailBody += "<tr><td>";
                            _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Renewal Sales</b></td>";
                            _emailBody += "</tr>";

                            _emailBody += "<tr>";
                            _emailBody += "<td><b>No.</b></td>";
                            _emailBody += "<td><b>InsuranceType</b></td>";
                            _emailBody += "<td><b>CompanyName</b></td>";
                            _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                            _emailBody += "</tr>";
                            int _No = 0;
                            for (int i = 0; i < _dsNewApplicationStatus.Tables[11].Rows.Count; i++)
                            {
                                _No += 1;
                                float _TotalPreium = 0.0f;
                                if (_dsNewApplicationStatus.Tables[11].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsNewApplicationStatus.Tables[11].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                    _TotalPreium = Convert.ToSingle(_dsNewApplicationStatus.Tables[11].Rows[i]["TotalCollectedPremium"].ToString());
                                _emailBody += "<tr>";
                                _emailBody += "<td>" + _No.ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[11].Rows[i]["InsuranceType"].ToString() + "</td>";
                                _emailBody += "<td>" + _dsNewApplicationStatus.Tables[11].Rows[i]["CompanyName"].ToString() + "</td>";
                                _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                                _emailBody += "</tr>";
                            }
                            _emailBody += "<tr>";
                            _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                            _emailBody += "<td width='150' align='right'><b>" + _dNewRenewalTotal.ToString("#,#0.00") + "</b></td>";
                            _emailBody += "</tr>";
                            _emailBody += "</table>";
                            _emailBody += "</td></tr>";
                            _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        }
                        #endregion

                        #region Daily and Monthly Total Collection
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Total Collection</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>No.</b></td>";
                        _emailBody += "<td><b>Description</b></td>";
                        _emailBody += "<td align='right'><b>VMI Only</b></td>";
                        _emailBody += "<td align='right'><b>VMI + CMI</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td style='color:#045FB4;'>1</td>";
                        _emailBody += "<td style='color:#045FB4;'><b>Daily Total Collection</td>";
                        if (_dDailyVMITotal >= 1000)
                        {
                            _emailBody += "<td style='color:#045FB4;' align='right'>" + _dDailyVMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td style='color:#045FB4;' align='right'>" + _dDailyVMITotal.ToString("#0.00") + "</td>";
                        }
                        if (_dDailywithCMITotal >= 1000)
                        {
                            _emailBody += "<td  style='color:#045FB4;' align='right'>" + _dDailywithCMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td  style='color:#045FB4;' align='right'>" + _dDailywithCMITotal.ToString("#0.00") + "</td>";
                        }
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td style='color:#0B6138;'>2</td>";
                        _emailBody += "<td style='color:#0B6138;'><b>Monthly Total Collection</td>";
                        if (_dMonthlyVMITotal >= 1000)
                        {
                            _emailBody += "<td style='color:#0B6138;' align='right'>" + _dMonthlyVMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td style='color:#0B6138;' align='right'>" + _dMonthlyVMITotal.ToString("#0.00") + "</td>";
                        }
                        if (_dMonthlywithCMITotal >= 1000)
                        {
                            _emailBody += "<td  style='color:#0B6138;' align='right'>" + _dMonthlywithCMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td  style='color:#0B6138;' align='right'>" + _dMonthlywithCMITotal.ToString("#0.00") + "</td>";
                        }
                        _emailBody += "</tr>";

                        // Cancel
                        _emailBody += "<tr>";
                        _emailBody += "<td style='color:#F00000;'>3</td>";
                        _emailBody += "<td style='color:#F00000;'><b>Monthly Total Cancel</td>";
                        if (_dMonthlyCancelVMITotal >= 1000)
                        {
                            _emailBody += "<td style='color:#F00000;' align='right'>" + _dMonthlyCancelVMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td style='color:#F00000;' align='right'>" + _dMonthlyCancelVMITotal.ToString("#0.00") + "</td>";
                        }
                        if (_dMonthlywithCMITotal >= 1000)
                        {
                            _emailBody += "<td  style='color:#F00000;' align='right'>" + _dMonthlyCancelwithCMITotal.ToString("#0,000.00") + "</td>";
                        }
                        else
                        {
                            _emailBody += "<td  style='color:#F00000;' align='right'>" + _dMonthlyCancelwithCMITotal.ToString("#0.00") + "</td>";
                        }
                        _emailBody += "</tr>";

                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                        #endregion

                        #region Online Channels
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>Online Channels</b></td>";
                        _emailBody += "<td align='right'><b>Daily</b></td>";
                        _emailBody += "<td align='right'><b>Monthly</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td style='color:#045FB4;'><b>Facebook</td>";
                        _emailBody += "<td style='color:#045FB4;' align='right'>" + _dFacebookDailyTotal.ToString() + "</td>";
                        _emailBody += "<td  style='color:#045FB4;' align='right'>" + _dFacebookMonthlyTotal.ToString() + "</td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td style='color:#0B6138;'><b>Google</td>";
                        _emailBody += "<td style='color:#0B6138;' align='right'>" + _dGoogleDailyTotal.ToString() + "</td>";
                        _emailBody += "<td  style='color:#0B6138;' align='right'>" + _dGoogleMonthlyTotal.ToString() + "</td>";
                        _emailBody += "</tr>";

                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        #endregion

                        _emailBody += "</table>";
                        _emailBody += "</body>";
                        _emailBody += "</html>";
                        #endregion

                        bool _emailSendNew = false;
                        while (!_emailSendNew)
                        {
                            if (MyMailManager.SendEmail(_emailTitle, _emailBody, _toemail, _ccemail, _bccemail))
                            {
                                _emailSendNew = true;
                                _eventLog.WriteEntry("GG Sales List Sent successfully [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.SuccessAudit);
                            }
                            else
                            {
                                timer1.Interval = 24 * 60 * 60 * 1000;
                                System.Threading.Thread.Sleep(30 * 60 * 1000);
                                _eventLog.WriteEntry("GG Sales List can't be Sent [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                            }
                        }
                    }
                    else
                    {
                        _eventLog.WriteEntry("No more GG Sales Today [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                    }
                }
                else
                {
                    _eventLog.WriteEntry("No more GG Sales Today [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                }
                #endregion

                #region OLd Format - Hide on 18 Feb 2014
                /*
                Int32 _FileCount = 0;
                MyData mydata = new MyData();
                DataSet _dsApplicationStatus = new DataSet();
                _dsApplicationStatus = mydata.GetApplicationStatusList();

                if (_dsApplicationStatus.Tables.Count > 1 && _dsApplicationStatus.Tables[1].Rows.Count > 0)
                {
                    _FileCount += _dsApplicationStatus.Tables[1].Rows.Count;
                }

                if (_dsApplicationStatus.Tables.Count > 2 && _dsApplicationStatus.Tables[2].Rows.Count > 0)
                {
                    _FileCount += _dsApplicationStatus.Tables[2].Rows.Count;
                }

                if (_dsApplicationStatus.Tables.Count > 3 && _dsApplicationStatus.Tables[3].Rows.Count > 0)
                {
                    _FileCount += _dsApplicationStatus.Tables[3].Rows.Count;
                }
                
                if (_dsApplicationStatus.Tables.Count > 4 && _dsApplicationStatus.Tables[4].Rows.Count > 0)//Add 30 May 2013 for Check Fresh/Old Leads sales
                {
                    _FileCount += _dsApplicationStatus.Tables[4].Rows.Count;
                }
                
                #region Daily and Monthly Total Collection - Values
                if (_dsApplicationStatus.Tables.Count > 5 && _dsApplicationStatus.Tables[5].Rows.Count > 0)
                {
                    if (_dsApplicationStatus.Tables[5].Rows[0]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[0]["Amount"].ToString() != string.Empty)
                    {
                        _strOnlineFreshTotal = _dsApplicationStatus.Tables[5].Rows[0]["Amount"].ToString();
                        if (!decimal.TryParse(_strOnlineFreshTotal, out _dOnlineFreshTotal))
                        {
                            _dOnlineFreshTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 1 && _dsApplicationStatus.Tables[5].Rows[1]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[1]["Amount"].ToString() != string.Empty)
                    {
                        _strOnlineOldTotal = _dsApplicationStatus.Tables[5].Rows[1]["Amount"].ToString();
                        if (!decimal.TryParse(_strOnlineOldTotal, out _dOnlineOldTotal))
                        {
                            _dOnlineOldTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 2 && _dsApplicationStatus.Tables[5].Rows[2]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[2]["Amount"].ToString() != string.Empty)
                    {
                        _strRenewalTotal = _dsApplicationStatus.Tables[5].Rows[2]["Amount"].ToString();
                        if (!decimal.TryParse(_strRenewalTotal, out _dRenewalTotal))
                        {
                            _dRenewalTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 3 && _dsApplicationStatus.Tables[5].Rows[3]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[3]["Amount"].ToString() != string.Empty)
                    {
                        _strOfflineTotal = _dsApplicationStatus.Tables[5].Rows[3]["Amount"].ToString();
                        if (!decimal.TryParse(_strOfflineTotal, out _dOfflineTotal))
                        {
                            _dOfflineTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 4 && _dsApplicationStatus.Tables[5].Rows[4]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[4]["Amount"].ToString() != string.Empty)
                    {
                        _strDailyTotal = _dsApplicationStatus.Tables[5].Rows[4]["Amount"].ToString();
                        if (!decimal.TryParse(_strDailyTotal, out _dDailyTotal))
                        {
                            _dDailyTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 5 && _dsApplicationStatus.Tables[5].Rows[5]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[5]["Amount"].ToString() != string.Empty)
                    {
                        _strMonthlyTotal = _dsApplicationStatus.Tables[5].Rows[5]["Amount"].ToString();
                        if (!decimal.TryParse(_strMonthlyTotal, out _dMonthlyTotal))
                        {
                            _dMonthlyTotal = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 6 && _dsApplicationStatus.Tables[5].Rows[6]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[6]["Amount"].ToString() != string.Empty)
                    {
                        _strDailyTotal = _dsApplicationStatus.Tables[5].Rows[6]["Amount"].ToString();
                        if (!decimal.TryParse(_strDailyTotal, out _dDailyTotalWithCMI))
                        {
                            _dDailyTotalWithCMI = 0;
                        }
                    }
                    if (_dsApplicationStatus.Tables[5].Rows.Count > 7 && _dsApplicationStatus.Tables[5].Rows[7]["Amount"] != DBNull.Value
                            && _dsApplicationStatus.Tables[5].Rows[7]["Amount"].ToString() != string.Empty)
                    {
                        _strMonthlyTotal = _dsApplicationStatus.Tables[5].Rows[7]["Amount"].ToString();
                        if (!decimal.TryParse(_strMonthlyTotal, out _dMonthlyTotalWithCMI))
                        {
                            _dMonthlyTotalWithCMI = 0;
                        }
                    }
                }
                #endregion 

                if (_FileCount > 0)
                {
                    _emailBody += "<html>";
                    _emailBody += "<head>";
                    _emailBody += "<title></title>";
                    _emailBody += "</head>";
                    _emailBody += "<body class='email_body'>";

                    _emailBody += "<style type='text/css'>";
                    _emailBody += ".email_body {background: white;margin: 0;padding: 0;color: #4c4c4c;font-family: Arial, Helvetica, sans-serif;font-size: 0.85em;}";
                    _emailBody += "a {text-decoration:underline;color:#2c95cc;} ";
                    _emailBody += "#email_container {margin:0 auto;padding:20px;width:600px; height:auto;} ";
                    _emailBody += "#email_body {width:100%; text-align: left !important;} ";
                    _emailBody += ".email_pad10px {padding:10px;} ";
                    _emailBody += ".email_results td {width:33%;} ";
                    _emailBody += "</style>";

                    _emailBody += "<table width='100%' border='0' cellspacing='2' cellpadding='0'>";
                    _emailBody += "<tr><td style='color:green; font-size:large;'><b>GG Sales at " + _dtmApplicationDate.ToString("dd/MMM/yyyy dddd") + "</b></td></tr>";
                    _emailBody += "<tr><td>&nbsp;</td></tr>";

                    #region Online Sales - NEW (Fresh Leads)
                    if (_dsApplicationStatus.Tables[1].Rows.Count > 0)
                    {
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Online Sales - NEW (Fresh Leads)</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>No.</b></td>";
                        _emailBody += "<td><b>InsuranceType</b></td>";
                        _emailBody += "<td><b>CompanyName</b></td>";
                        _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                        _emailBody += "</tr>";
                        int _SNo = 0;
                        //float _GrandTotal = 0.0f;
                        for (int i = 0; i < _dsApplicationStatus.Tables[1].Rows.Count; i++)
                        {
                            //TotalPremium changed to TotalCollectedPremium for Sales @ 30 May 2013
                            _SNo += 1;
                            float _TotalPreium = 0.0f;
                            if (_dsApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                _TotalPreium = Convert.ToSingle(_dsApplicationStatus.Tables[1].Rows[i]["TotalCollectedPremium"].ToString());
                            //_GrandTotal += _TotalPreium;
                            _emailBody += "<tr>";
                            _emailBody += "<td>" + _SNo.ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[1].Rows[i]["InsuranceType"].ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[1].Rows[i]["CompanyName"].ToString() + "</td>";
                            _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                            _emailBody += "</tr>";
                        }
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                        _emailBody += "<td width='150' align='right'><b>" + _dOnlineFreshTotal.ToString("#,#0.00") + "</b></td>";
                        _emailBody += "</tr>";
                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                    }
                    #endregion

                    #region Online Sales - NEW (Last Year Leads)
                    if (_dsApplicationStatus.Tables[4].Rows.Count > 0)
                    {
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Online Sales - NEW (Old Leads - More then 6 months)</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>No.</b></td>";
                        _emailBody += "<td><b>InsuranceType</b></td>";
                        _emailBody += "<td><b>CompanyName</b></td>";
                        _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                        _emailBody += "</tr>";
                        int _SNo = 0;
                        //float _GrandTotal = 0.0f;
                        for (int i = 0; i < _dsApplicationStatus.Tables[4].Rows.Count; i++)
                        {
                            _SNo += 1;
                            float _TotalPreium = 0.0f;
                            if (_dsApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"] != DBNull.Value && _dsApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"].ToString() != string.Empty)
                                _TotalPreium = Convert.ToSingle(_dsApplicationStatus.Tables[4].Rows[i]["TotalCollectedPremium"].ToString());
                            //_GrandTotal += _TotalPreium;
                            _emailBody += "<tr>";
                            _emailBody += "<td>" + _SNo.ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[4].Rows[i]["InsuranceType"].ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[4].Rows[i]["CompanyName"].ToString() + "</td>";
                            _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                            _emailBody += "</tr>";
                        }
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                        _emailBody += "<td width='150' align='right'><b>" + _dOnlineOldTotal.ToString("#,#0.00") + "</b></td>";
                        _emailBody += "</tr>";
                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                    }
                    #endregion

                    #region Online Sales - RENEWAL
                    if (_dsApplicationStatus.Tables[3].Rows.Count > 0)
                    {
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";

                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>RENEWAL</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>No.</b></td>";
                        _emailBody += "<td><b>InsuranceType</b></td>";
                        _emailBody += "<td><b>CompanyName</b></td>";
                        _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                        _emailBody += "</tr>";
                        int _SNo = 0;
                        //float _GrandTotal = 0.0f;
                        for (int k = 0; k < _dsApplicationStatus.Tables[3].Rows.Count; k++)
                        {
                            _SNo += 1;
                            float _TotalPreium = 0.0f;
                            if (_dsApplicationStatus.Tables[3].Rows[k]["TotalCollectedPremium"] != DBNull.Value && _dsApplicationStatus.Tables[3].Rows[k]["TotalCollectedPremium"].ToString() != string.Empty)
                                _TotalPreium = Convert.ToSingle(_dsApplicationStatus.Tables[3].Rows[k]["TotalCollectedPremium"].ToString());
                            //_GrandTotal += _TotalPreium;
                            _emailBody += "<tr>";
                            _emailBody += "<td>" + _SNo.ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[3].Rows[k]["InsuranceType"].ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[3].Rows[k]["CompanyName"].ToString() + "</td>";
                            _emailBody += "<td align='right'>" + _TotalPreium.ToString("#,#0.00") + "</td>";
                            _emailBody += "</tr>";
                        }
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                        _emailBody += "<td width='150' align='right'><b>" + _dRenewalTotal.ToString("#,#0.00") + "</b></td>";
                        _emailBody += "</tr>";
                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                    }
                    #endregion

                    #region Offline Sales
                    if (_dsApplicationStatus.Tables[2].Rows.Count > 0)
                    {
                        _emailBody += "<tr><td>";
                        _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Offline Sales</b></td>";
                        _emailBody += "</tr>";

                        _emailBody += "<tr>";
                        _emailBody += "<td><b>No.</b></td>";
                        _emailBody += "<td><b>InsuranceType</b></td>";
                        _emailBody += "<td><b>CompanyName</b></td>";
                        _emailBody += "<td align='right'><b>TotalPremium</b></td>";
                        _emailBody += "</tr>";
                        int _OffSNo = 0;
                        //float _OffGrandTotal = 0.0f;
                        for (int j = 0; j < _dsApplicationStatus.Tables[2].Rows.Count; j++)
                        {
                            _OffSNo += 1;
                            float _OffTotalPreium = 0.0f;
                            if (_dsApplicationStatus.Tables[2].Rows[j]["TotalCollectedPremium"] != DBNull.Value && _dsApplicationStatus.Tables[2].Rows[j]["TotalCollectedPremium"].ToString() != string.Empty)
                                _OffTotalPreium = Convert.ToSingle(_dsApplicationStatus.Tables[2].Rows[j]["TotalCollectedPremium"].ToString());
                            //_OffGrandTotal += _OffTotalPreium;
                            _emailBody += "<tr>";
                            _emailBody += "<td>" + _OffSNo.ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[2].Rows[j]["InsuranceType"].ToString() + "</td>";
                            _emailBody += "<td>" + _dsApplicationStatus.Tables[2].Rows[j]["CompanyName"].ToString() + "</td>";
                            _emailBody += "<td align='right'>" + _OffTotalPreium.ToString("#,#0.00") + "</td>";
                            _emailBody += "</tr>";
                        }
                        _emailBody += "<tr>";
                        _emailBody += "<td colspan='3' align='right'><b>Grand Total</b></td>";
                        _emailBody += "<td width='150' align='right'><b>" + _dOfflineTotal.ToString("#,#0.00") + "</b></td>";
                        _emailBody += "</tr>";
                        _emailBody += "</table>";
                        _emailBody += "</td></tr>";
                        _emailBody += "<tr><td colspan='4'>&nbsp;</td></tr>";
                    }
                    #endregion

                    #region Daily and Monthly Total Collection
                    _emailBody += "<tr><td>";
                    _emailBody += "<table width='100%' border='1' cellspacing='2' cellpadding='0'>";
                    _emailBody += "<tr>";
                    _emailBody += "<td colspan='4' style='background: #E6E6E6;'><b>Total Collection</b></td>";
                    _emailBody += "</tr>";

                    _emailBody += "<tr>";
                    _emailBody += "<td><b>No.</b></td>";
                    _emailBody += "<td><b>Description</b></td>";
                    _emailBody += "<td align='right'><b>VMI Only</b></td>";
                    _emailBody += "<td align='right'><b>VMI + CMI</b></td>";
                    _emailBody += "</tr>";

                    _emailBody += "<tr>";
                    _emailBody += "<td style='color:#045FB4;'>1</td>";
                    _emailBody += "<td style='color:#045FB4;'><b>Daily Total Collection</td>";
                    if (_dDailyTotal >= 1000)
                    {
                        _emailBody += "<td style='color:#045FB4;' align='right'>" + _dDailyTotal.ToString("#0,000.00") + "</td>";
                    }
                    else
                    {
                        _emailBody += "<td style='color:#045FB4;' align='right'>" + _dDailyTotal.ToString("#0.00") + "</td>";
                    }
                    if (_dDailyTotalWithCMI >= 1000)
                    {
                        _emailBody += "<td  style='color:#045FB4;' align='right'>" + _dDailyTotalWithCMI.ToString("#0,000.00") + "</td>";
                    }
                    else
                    {
                        _emailBody += "<td  style='color:#045FB4;' align='right'>" + _dDailyTotalWithCMI.ToString("#0.00") + "</td>";
                    }
                    _emailBody += "</tr>";

                    _emailBody += "<tr>";
                    _emailBody += "<td style='color:#0B6138;'>2</td>";
                    _emailBody += "<td style='color:#0B6138;'><b>Monthly Total Collection</td>";
                    if (_dDailyTotal >= 1000)
                    {
                        _emailBody += "<td style='color:#0B6138;' align='right'>" + _dMonthlyTotal.ToString("#0,000.00") + "</td>";
                    }
                    else
                    {
                        _emailBody += "<td style='color:#0B6138;' align='right'>" + _dMonthlyTotal.ToString("#0.00") + "</td>";
                    }
                    if (_dDailyTotalWithCMI >= 1000)
                    {
                        _emailBody += "<td  style='color:#0B6138;' align='right'>" + _dMonthlyTotalWithCMI.ToString("#0,000.00") + "</td>";
                    }
                    else
                    {
                        _emailBody += "<td  style='color:#0B6138;' align='right'>" + _dMonthlyTotalWithCMI.ToString("#0.00") + "</td>";
                    }
                    _emailBody += "</tr>";

                    _emailBody += "</table>";
                    _emailBody += "</td></tr>";
                    //if (_dDailyTotal >= 1000)
                    //{
                    //    _emailBody += "<tr><td style='color:#045FB4; font-size:16px;'><b>Daily Total Collection : " + _dDailyTotal.ToString("#0,000.00") + "</b></td></tr>";
                    //    _emailBody += "<br/>";
                    //}
                    //else
                    //{
                    //    _emailBody += "<tr><td style='color:#045FB4; font-size:16px;'><b>Daily Total Collection : " + _dDailyTotal.ToString("#0.00") + "</b></td></tr>";
                    //    _emailBody += "<br/>";
                    //}
                    //if (_dMonthlyTotal >= 1000)
                    //{
                    //    _emailBody += "<tr><td style='color:#0B6138; font-size:16px;'><b>Monthly Total Collection : " + _dMonthlyTotal.ToString("#0,000.00") + "</b></td></tr>";
                    //    _emailBody += "<br/>";
                    //}
                    //else
                    //{
                    //    _emailBody += "<tr><td style='color:#0B6138; font-size:16px;'><b>Monthly Total Collection : " + _dMonthlyTotal.ToString("#00.00") + "</b></td></tr>";
                    //    _emailBody += "<br/>";
                    //}
                    #endregion

                    _emailBody += "</table>";
                    _emailBody += "</body>";
                    _emailBody += "</html>";

                    bool _emailSend = false;
                    while (!_emailSend)
                    {
                        if (MyMailManager.SendEmail(_emailTitle, _emailBody, _toemail, _ccemail, _bccemail))
                        {
                            _emailSend = true;
                            _eventLog.WriteEntry("GG Sales List Sent successfully [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.SuccessAudit);
                        }
                        else
                        {
                            timer1.Interval = 24 * 60 * 60 * 1000;
                            System.Threading.Thread.Sleep(30 * 60 * 1000);
                            _eventLog.WriteEntry("GG Sales List can't be Sent [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Error);
                        }
                    }
                }
                else
                {
                    _eventLog.WriteEntry("No more GG Sales Today [" + GG.Global.General.GetUTCDateTime().ToString() + "].", EventLogEntryType.Information);
                }
                */
                #endregion
            }
            catch (Exception Ex)
            {
                _eventLog.WriteEntry("GG Sales List Sending Error" + Ex.Message, EventLogEntryType.Error);
                CreateExceptionLog(Ex);
            }
        }

        private static void CreateExceptionLog(Exception Ex)
        {
            String[] _fileContent = new String[6];
            _fileContent[0] = "Source : " + Ex.Source;
            _fileContent[1] = "Date Time : " + GG.Global.General.GetUTCDateTime().ToString();
            _fileContent[2] = "Stack Trace :" + Ex.StackTrace.ToString();
            _fileContent[3] = "Target Site : " + Ex.TargetSite.ToString();
            _fileContent[4] = "Location : " + "AppStatusSending";
            _fileContent[5] = "Message : " + Ex.Message;

            WriteExceptionLog("AppStatus_", _fileContent);
        }

        private static void WriteExceptionLog(string strPrefixFileName, string[] strException)
        {
            try
            {
                GG.InputOut.MyIO _newFile = new GG.InputOut.MyIO();
                string _filePath = AppDomain.CurrentDomain.BaseDirectory + "Exception\\";
                if (_newFile.CheckDirectory(_filePath))
                {
                    string _curMonth = GG.Global.General.GetUTCDateTime().Year.ToString("0000") + GG.Global.General.GetUTCDateTime().Month.ToString("00");

                    if (!_newFile.CheckDirectory(_filePath + _curMonth))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curMonth))
                        {
                            _filePath += _curMonth + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curMonth + "\\";
                    }

                    string _fileName = strPrefixFileName + _curMonth + GG.Global.General.GetUTCDateTime().Day.ToString("00") + GG.Global.General.GetUTCDateTime().Second.ToString("00") + GG.Global.General.GetUTCDateTime().Millisecond.ToString("00") + ".txt";

                    _newFile.WriteNewFile(_filePath, _fileName, strException);
                    _newFile = null;
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
        }

    }
}
